<?php
/***************栏目操作***************/
class categoryControl extends Control{
	function index(){//栏目显示
		C("debug",0);
	$db=M("category");
	$row=$db->all();
	$row=arr::channel($row,"cateid","pid",0,2,"&nbsp;");
	$this->assign("row",$row);
	$this->display();
	}
	
	function add_show(){
	$cateid=isset($_GET['cateid'])?$_GET['cateid']:'0';
	$cate=M("category");
	$cate=$cate->field("cateid,level,pid")->where("cateid=".$cateid)->all();
	$this->assign("cate",$cate);
	$db=M("role");
	$row=$db->all();
	$this->assign("row",$row);
	$this->display();
	}


	function add(){//栏目添加
	if(!empty($_POST['cname'])){
		$db=M("category");
		$_POST['level']=$_POST['level']+1;
		if($db->add()){
		 $this->category_access_set($db->get_insert_id());//获取到最后插入的主键然后执行栏目权限添加
		$this->success("栏目新增成功 <a href='__APP__?c=theme&m=add_show'>添加文章</a><a href='__APP__'>返回首页</a>");
		}
		}else{
		$this->error("栏目名不能为空");
		}
		}									
		function category_access_set($cateid){
			if(!$_POST['category_access']){
				return;}
				$category_access=$_POST['category_access'];
					$m=M("category_access");
				foreach ($category_access as $key => $v) {
					if(empty($v['show'])){
						continue;
					}
					$v['cateid']=$cateid;
					$m->add($v);
		//add方法如果没指定参数的话就默认为$_POST如果指定了参数就添加参数中的内容
			}
	}
	function del(){//栏目删除
		$catid=$_POST['data'];
		$db=M("category");
		$row=$db->where("pid='$catid'")->find();
		if($db->get_affected_rows()){//如果有受到影响的话就代表有子栏目不处理
				echo 3;
		}else{
			if($db->where("cateid='$catid'")->del()){//没有子栏目就删除成功返回1不成功返回2
			echo 1;
			}else{
			echo 2;
			}
		}
		
	}
	function add_type(){
			$cname=$_POST['data'];
			$db=M("category");
			$row=$db->where("cname='$cname'")->find();
			if($row){
			echo 2;
			}else{
			echo 1;
			}
	}

	function selecttemplate(){
		C("debug",1);
		$path=C("tpl_index_dir").'/'.C('tplstyle');
		$files=dir::tree($path,'html');//获取格式为HTML的文件
		$this->assign("tplpth",$path);
		$this->assign("files",$files);
		$this->display();



	}	



}
?>